package python

import (
	"awesomeTask/application/client"
	"awesomeTask/application/models/packge"
	"awesomeTask/application/models/programe"
	"awesomeTask/application/variable"
	"awesomeTask/application/websocket"
	awesomeTask "awesomeTask/commom"
	"awesomeTask/commom/StringHelper"
	"awesomeTask/system/http"
	"awesomeTask/system/rpc"
	"strings"
	"time"
)

type ProgrameHandler struct {
	http.HttpBaseHandler
}

func (handler *ProgrameHandler) GetModuleName() string {
	return "/programe/"
}
func (handler ProgrameHandler) ActionIndex() string {
	return "jjawesome"
}
func (handler ProgrameHandler) ActionEdit() interface{} {
	fileName := handler.PostOne("filename").(string)
	fileContent := handler.PostOne("programe_source").(string)
	fileContent = strings.ReplaceAll(fileContent, "	", "    ")
	dirName := handler.PostOne("dirname").(string)
	projectId := handler.PostOne("project_id").(string)
	pid := handler.PostOne("pid").(string)
	id := handler.PostOne("id").(string)
	uid := handler.getUserInfo()["id"].(string)
	create_time := handler.PostOne("create_time")
	if create_time == nil {
		create_time = awesomeTask.Now()
	}
	if programe.EditPrograme(fileName, fileContent, dirName, projectId, id, uid, uid, pid, create_time.(string)) {
		return map[string]interface{}{
			"code": 200,
			"msg":  "保存成功",
		}
	}
	return map[string]interface{}{
		"code": 500,
		"msg":  "保存失败",
	}

}
func (handler ProgrameHandler) ActionTest() interface{} {
	time.Sleep(10000000)
	return map[string]interface{}{}
}
func (handler ProgrameHandler) ActionDetail() interface{} {
	return programe.GetProgrameById(handler.Get("id").(string))
}
func (handler ProgrameHandler) getUserInfo() map[string]interface{} {
	return map[string]interface{}{
		"id":  "1",
		"gid": "1",
	}
}
func (handler ProgrameHandler) ActionRun() map[string]interface{} {
	id := handler.PostOne("id").(string)
	taskInfo := programe.GetTaskBaseInfo(id)
	taskInfo["uniqueTaskId"] = StringHelper.UniqueId(10)
	go func() {
		res, err := rpc.RpcCall(variable.ON_TASK_REMOTE_FORK_TO_MASTER, taskInfo, client.GetServerCon(), 5000)
		if err != nil {
			websocket.SendToAll(variable.ON_LOG, err.Error())
		} else {
			value, ok := res.(map[string]interface{})["errno"]
			if ok && value == variable.TASK_FORK_ERRNO {
				websocket.SendToAll(variable.ON_LOG, "系统资源不足进程掉起失败")
				return
			}
			content := res.(map[string]interface{})["content"].(map[string]interface{})
			msg := ""
			if content["result"] == nil {
				msg += "任务开始时间:" + content["startAt"].(string) + "\n"
				msg += "任务结束时间:" + content["endAt"].(string) + "\n"
				msg += "执行结果:" + "" + "\n"
				if content["error"] != nil {
					msg += "错误信息:" + content["error"].(string) + "\n"
				}
			} else {
				msg += "任务开始时间:" + content["startAt"].(string) + "\n"
				msg += "任务结束时间:" + content["endAt"].(string) + "\n"
				msg += "执行结果:" + content["result"].(string) + "\n"
				msg += "错误信息:" + "" + "\n"
			}

			websocket.SendToAll(variable.ON_LOG, msg)
		}
	}()
	return map[string]interface{}{
		"code": 200,
		"msg":  "任务已经投递到集群",
	}
}
func (handler ProgrameHandler) ActionPackge() map[string]interface{} {
	return map[string]interface{}{
		"list": packge.GetPackge(handler.Get("project_id").(string)),
	}
}

func (handler ProgrameHandler) ActionEditPackge() map[string]interface{} {
	if packge.EditOrInsert(handler.PostMany()) {
		return map[string]interface{}{
			"msg": "保存成功",
		}
	} else {
		return map[string]interface{}{
			"msg":  "保存失败",
			"code": 500,
		}
	}
}

//func (handler ProgrameHandler) ActionInstall() map[string]interface{} {
//	projectId := handler.PostOne("project_id")
//	return
//}

/**
对外提供调用程序的hook
*/
func (handler ProgrameHandler) ActionProcessHook() map[string]interface{} {
	id := handler.PostOne("id").(string) //程序入口文件id 必传
	taskInfo := programe.GetTaskBaseInfo(id)
	taskInfo["uniqueTaskId"] = StringHelper.UniqueId(10)
	inputParams := handler.PostOne("params")
	taskInfo["extendParams"] = inputParams
	res, err := rpc.RpcCall(variable.ON_TASK_REMOTE_FORK_TO_MASTER, taskInfo, client.GetServerCon(), 5000)
	if err != nil {
		return map[string]interface{}{
			"error": err.Error(),
		}
	}
	return map[string]interface{}{
		"result": res,
		"errno":  0,
	}
}
